from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException


#wd = webdriver.Chrome(service=Service(r'C:\zj3j\webdriver\101\chromedriver.exe'))

# 根据title名称切换标签页
def switch_tabs_by_title(title_name, wd):
    handles = wd.window_handles
    for handle in handles:  # 历遍所有标签的句柄
        wd.switch_to.window(handle)  # 通过句柄切换到该浏览器的标签
        #print("标签页-" + wd.title)
        if title_name in wd.title:    #driver.title当前标签名
            #print('切换标签页，当前标签页标题名为： ' + wd.title)
            break #退出循环

def check_element_exist(wd, xpaths):
    """
    判断是否存在元素
    :param wd: 浏览器对象
    :param xpaths: xpaths表达式
    :return: True or False
    """
    try:
        target = wd.find_element(By.XPATH, xpaths)
    except NoSuchElementException:
        #print('元素不存在')
        return False
    else:
        #print('元素存在')
        return True

def switch_to_first_table(wd): #跳转到最新的页面
    wd.switch_to.window(wd.window_handles[-1])

if __name__ == '__main__':
    print('')
    #switch_to_first_table(wd)